 Ext.define('Admin.view.system.msg.MsgController', {
   
	extend: 'Admin.ux.GridController',
  
    alias: 'controller.msg',
    //选择记录
    onSelectionChange : function(grid, selection){
    	
    	var viewModel = this.getView().getViewModel();
    	var records = grid.getSelection();
    	
    	if(records.length >=1){
    		viewModel.set('batchRemoveAction',false);
    		viewModel.set('batchRemoveText',"批量删除("+records.length+")");
    		viewModel.set('batchConfirmAction',false);
    		viewModel.set('batchConfirmText',"确认消息("+records.length+")");
    	}else{
    		viewModel.set('batchRemoveAction',true);
    		viewModel.set('batchRemoveText',"批量删除(0)");
    		viewModel.set('batchConfirmAction',true);
    		viewModel.set('batchConfirmText',"确认消息(0)");
    	}
    },
    //地点列表
	placeAction:function(b){
    	var win = Ext.create("Admin.ux.Window",{
    		title:'常用地点',
    		width:'35%',
    		height:'75%',
    		animateTarget: b,
    		items:{xtype:'place-view'},
    	});	
	},
    //新增
    addAction: function (b) {
    	var me = this;
    	var win = Ext.create("Admin.ux.Window",{
    		title:'新建消息',
    		width:'50%',
			height:'100%',
    		animateTarget: b,
    		items:{xtype:'msg-form'},
    		buttons:[	
    			{
    				text:'保存',
    				ui: 'soft-green',
    				iconCls: 'x-fa 	fa-floppy-o',
    				handler:function(btn){
    					var form = btn.up("window").down("form").getForm();
    					
    					var treeView = win.down('treepanel');
    					var records = treeView.getChecked();
				        
				        var _userIds = [];
				        Ext.Array.each(records, function(rec){
				        	if(rec.get('id')!='root' && rec.get('id')!='' && rec.get('id').indexOf('extModel')<=-1 ){
				        		_userIds.push(rec.get('id'));
				        	}	
				        });
				       
						Ext.create('Admin.ux.Action').submit(form,'/msg/add?_userIds='+_userIds,me.getView(),win);
    				}
    			},
    			{
    				text:'重置',
    				ui: 'soft-red',
    				iconCls: 'x-fa 	fa-mail-reply',
    				handler:function(btn){
    					btn.up("window").down("form").reset();
    				}
    			},
    			{
    				text:'取消',
    				ui: 'gray',
    				iconCls: 'x-fa 	fa-ban',
    				handler:function(btn){
    					btn.up("window").close();
    				}
    			}
    		]
    	});
    },
    //编辑
    editAction:function(grid, rowIndex, colIndex,item,e,record,row){
    	
    	var rec = grid.getStore().getAt(rowIndex);
    	
    	var me = this;
    	
    	var win = Ext.create("Admin.ux.Window",{
    		title:'编辑消息',
    		animateTarget: row,
    		width:710,
			height:550,
    		items:{xtype:'msg-form'},
    		animateTarget : row,
    		buttons:[	
    			{
    				text:'保存',
    				ui: 'soft-green',
    				iconCls: 'x-fa 	fa-floppy-o',
    				handler:function(btn){
    					var form = btn.up("window").down("form").getForm();
    					var treeView = win.down('treepanel');
    					var records = treeView.getChecked();
				        
				        var _userIds = [];
				        Ext.Array.each(records, function(rec){
				        	if(rec.get('id')!='root' && rec.get('id')!='' && rec.get('id').indexOf('extModel')<=-1 ){
				        		_userIds.push(rec.get('id'));
				        	}	
				        });
    					
						Ext.create('Admin.ux.Action').submit(form,'/msg/edit?_userIds='+_userIds,me.getView(),win);
    				}
    			},
    			{
    				text:'重置',
    				ui: 'soft-red',
    				iconCls: 'x-fa 	fa-mail-reply',
    				handler:function(btn){
    					var form = btn.up("window").down("form");
    					form.loadRecord(rec);
    				}
    			},
    			{
    				text:'取消',
    				ui: 'gray',
    				iconCls: 'x-fa 	fa-ban',
    				handler:function(btn){
    					btn.up("window").close();
    				}
    			}
    		],
    		
    		listeners:{
    			afterrender:function(){
    				var tree = this.down('treepanel');
    				me.loadTree(tree,rec.get('id'));
    			},
    			show:function(w,opts){
    				var form = this.down('form');
    				form.getForm().load({
                        url: '/msg/get',
                        waitMsg:'加载中...',
                        params: {
                             id: rec.get('id')
                        },
                     });
    			}
    		}
    	});
    	
    },
    //加载树
    loadTree : function(authView,_msgId){
    	var treeView =authView;
		var viewMode  = treeView.getViewModel( );
		var store = viewMode.getStore('authStores');
		
		var myMask = new Ext.LoadMask({
		    msg    : '加载中数据中...',
		    target : treeView
		});
		myMask.show();
		
		Ext.apply(store.proxy.extraParams,{msg_id :_msgId});
	   
		store.on('load',function(st, records, successful, operation, node, eOpts){
			if(successful){
				myMask.hide();
			}
		});
	   store.load();
    },
    //删除行
    removeAction:function(grid, rowIndex, colIndex,item,e,record,row){
    	var rec = grid.getStore().getAt(rowIndex);
		var _id = rec.data.id;
		Ext.create('Admin.ux.Action').remove({id:_id},'/msg/delete',this.getView(),row);
    },
    //批量删除
    batchRemoveAction:function(b){
    	var grid = this.getView();
    	var data = grid.getSelectionModel().getSelection();
		var arr = [];
		Ext.each(data,function(obj,index,countriesItSelf){
			Ext.Array.include(arr,obj.get('id'));
		});
		Ext.create('Admin.ux.Action').remove({ids:arr},'/msg/deleteBatch',grid,b);
    },
    //批量确认
    batchConfirmAction:function(b){
    	var grid = this.getView();
    	var data = grid.getSelectionModel().getSelection();
		var arr = [];
		Ext.each(data,function(obj,index,countriesItSelf){
			Ext.Array.include(arr,obj.get('id'));
		});
		Ext.create('Admin.ux.Action').doAction({
			url:'/msg/confirmBatch',
			params:{ids:arr},
			grid:grid,
			msg:'确认消息?'
		});
    },
    //发送消息
    SendAction:function(grid, rowIndex, colIndex,item,e,record,row){
    	
    	var rec = grid.getStore().getAt(rowIndex);
		var _id = rec.data.id;
		var content  = rec.data.msg_content
		Ext.create('Admin.ux.Action').doAction({
			url:'/msg/sendMessage',
			params:{id:_id},
			grid:grid,
			msg:'确认发送内容为:'+content+"   的消息?"
		});
    },
    //设置定时器
    CronAction:function(grid, rowIndex, colIndex,item,e,record,row){
    	var me = this;
    	var win = Ext.create("Admin.ux.Window",{
    		title:'设置定时器',
    		width:'50%',
			height:'45%',
    		items:{xtype:'job-form'},
    		buttons:[	
    			{
    				text:'保存',
    				ui: 'soft-green',
    				iconCls: 'x-fa 	fa-floppy-o',
    				handler:function(btn){
    					var form = btn.up("window").down("form").getForm();
    					var rec = grid.getStore().getAt(rowIndex);
    					var msg_id = rec.data.id;
						Ext.create('Admin.ux.Action').submit(form,'/job/add?msg_id='+msg_id,me.getView(),win);
    				}
    			},
    		]
    	});
    }
});
